package com.jz.digital.zhixiangruku

import android.app.Activity
import android.content.Intent
import android.content.res.ColorStateList
import android.graphics.Color
import android.view.Gravity
import android.view.View
import android.widget.PopupWindow
import com.jz.digital.R
import com.jz.digital.base.BaseClickActivity
import com.jz.digital.bean.ServerBean
import com.jz.digital.dialog.DialogUtil
import com.jz.digital.utils.systembar.ImmersionBar
import kotlinx.android.synthetic.main.activity_zhi_xiang_pre_layout.label_team_class
import kotlinx.android.synthetic.main.activity_zhi_xiang_pre_layout.label_team_group
import kotlinx.android.synthetic.main.activity_zhi_xiang_pre_layout.label_warehouse_class
import kotlinx.android.synthetic.main.activity_zhi_xiang_ruku_layout.label_scan
import kotlinx.android.synthetic.main.activity_zhi_xiang_ruku_layout.tv_commit
import kotlinx.android.synthetic.main.layout_common_title.layout_parent

/**
 * @author jiangdz
 * @date 2024/11/19
 * @time 19:26
 * 用途:纸箱入库
 */
class ZhiXiangRuKuCommitActivity:BaseClickActivity<ZhiXiangRuKuCommitPresenter>(),
    IZhiXiangRuKuCommitView {
    private var mCanBiePopupWindow: PopupWindow? = null
    private var mTeamGroupPopupWindow: PopupWindow? = null
    private var mTeamClassPopupWindow: PopupWindow? = null

    override fun initData() {
        super.initData()
        mPresenter.initData()
    }

    override fun clickAction(view: View) {
        when(view.id){
            //仓别
            R.id.label_warehouse_class -> {
                showCanBieDialog()
            }
            //班组
            R.id.label_team_group -> {
                showTeamGroupDialog()

            }
            //班别
            R.id.label_team_class -> {
                showTeamClassDialog()
            }
            R.id.tv_commit->{

            }
        }
    }

    override fun getLayoutResId(): Int {
        return R.layout.activity_zhi_xiang_ruku_layout
    }

    override fun initViews() {
        super.initViews()
        ImmersionBar.with(this).statusBarColor(R.color.main_color)
            .titleBarMarginTop(layout_parent)
            .init()
        setTitle(R.string.zhixiang_ruku)
        mTitleBar.getTitleLayoutAll(this).setBackgroundColor(getColor(R.color.main_color))
        mTitleBar.setLeftImgTint(ColorStateList.valueOf(Color.WHITE))
        mTitleBar.setTitleColor(Color.WHITE)
        label_scan?.getRightImage()?.setOnClickListener {
        }
        label_scan?.getContentWidget()?.gravity= Gravity.START
        tv_commit?.setOnClickListener(this)
        label_team_group?.setOnClickListener(this)
        label_team_class?.setOnClickListener(this)
        label_warehouse_class?.setOnClickListener(this)
    }

    private fun dismissDialog() {
        mCanBiePopupWindow?.dismiss()
        mTeamGroupPopupWindow?.dismiss()
        mTeamClassPopupWindow?.dismiss()
    }

    private fun showCanBieDialog() {
        if (mCanBiePopupWindow?.isShowing == true) {
            return
        }
        if (mCanBiePopupWindow == null) {
            mCanBiePopupWindow = getNormalPopupWindow(mPresenter.mCanBieList) {
                label_warehouse_class?.getContentWidget()?.text=mPresenter.mTeamGroupList[it].basName
                mCanBiePopupWindow?.dismiss()
            }
        }
        mCanBiePopupWindow?.showAsDropDown(
            label_warehouse_class.getContentWidget(),
            0,
            0,
            Gravity.BOTTOM.or(Gravity.END)
        )
    }

    private fun showTeamGroupDialog() {
        if (mTeamGroupPopupWindow?.isShowing == true) {
            return
        }
        if (mTeamGroupPopupWindow == null) {
            mTeamGroupPopupWindow = getNormalPopupWindow(mPresenter.mTeamGroupList) {
                label_team_group?.getContentWidget()?.text=mPresenter.mTeamGroupList[it].basName
                mTeamGroupPopupWindow?.dismiss()
            }
        }
        mTeamGroupPopupWindow?.showAsDropDown(
            label_team_group.getContentWidget(),
            0,
            0,
            Gravity.BOTTOM.or(Gravity.END)
        )
    }

    private fun showTeamClassDialog() {
        if (mTeamClassPopupWindow?.isShowing == true) {
            return
        }
        if (mTeamClassPopupWindow == null) {
            mTeamClassPopupWindow = getNormalPopupWindow(mPresenter.mTeamClassList) {
                label_team_class?.getContentWidget()?.text=mPresenter.mTeamGroupList[it].basName
                mTeamClassPopupWindow?.dismiss()
            }
        }
        mTeamClassPopupWindow?.showAsDropDown(
            label_team_class.getContentWidget(),
            0,
            0,
            Gravity.BOTTOM.or(Gravity.END)
        )
    }

    private fun getNormalPopupWindow(
        dataList: List<ServerBean.GXInfo>,
        callback: (index: Int) -> Unit
    ): PopupWindow {
        return DialogUtil.getNormalPop(this, dataList, callback)
    }

    companion object {
        @JvmStatic
        fun startActivity(activity: Activity) {
            Intent(activity, ZhiXiangRuKuCommitActivity::class.java)
                .apply {
                    activity.startActivity(this)
                }
        }
    }

    override fun showLoading() {
        showLoadingDialog()
    }

    override fun dismissLoading() {
        dismissLoadingDialog(true)
    }
}